package com.guoning.office_system.service.impl;

import com.guoning.office_system.mapper.DepartmentMapper;
import com.guoning.office_system.mapper.UserMapper;
import com.guoning.office_system.model.Department;
import com.guoning.office_system.model.DepartmentWithUser;
import com.guoning.office_system.model.User;
import com.guoning.office_system.service.DepartmentService;
import com.guoning.office_system.vo.Criteria;
import com.guoning.office_system.vo.PageBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class DepartmentServiceImpl implements DepartmentService {
    @Autowired
    DepartmentMapper departmentMapper;

    @Autowired
    UserMapper userMapper;

    @Override
    public List<Department> selectAll(Criteria criteria) {
        return departmentMapper.selectAll(criteria);
    }

    @Override
    public PageBean selectByCondition(Criteria criteria, PageBean pageBean) {
        criteria.addExtField("pageIndex", pageBean.getPageIndex()*pageBean.getPageSize());
        criteria.addExtField("pageSize", pageBean.getPageSize());
        List<DepartmentWithUser> list = departmentMapper.selectByCondition(criteria);
        for (int i=0; i<list.size(); i++){
            User user = userMapper.selectByPrimaryKey(list.get(i).getUserId());
            user.setLoginPassword(null);
            user.setLoginErrorNum(null);
            user.setIsOnline(null);
            user.setEnableStatus(null);
            user.setLoginLockedTime(null);
            list.get(i).setUser(user);
        }
        Long count = departmentMapper.selectByConditionCount(criteria);
        pageBean.setResultList(list);
        pageBean.setTotalCount(count);
        return pageBean;
    }

    @Override
    public int updateFordelete(Criteria criteria) {
        return departmentMapper.updateFordelete(criteria);
    }

    @Override
    public int deleteByPrimaryKey(Long id) {
        return 0;
    }

    @Override
    public int insert(Department record) {
        return 0;
    }

    @Override
    public Long insertSelective(Department record) {
        Date now = new Date();
        record.setStatus(2);
        record.setCreateTime(now);
        record.setDr(0);
        record.setTs(now);
        departmentMapper.insertSelective(record);
        return record.getId();
    }

    @Override
    public Department selectByPrimaryKey(Long id) {
        return departmentMapper.selectByPrimaryKey(id);
    }

    @Override
    public int updateByPrimaryKeySelective(Department record) {
        return departmentMapper.updateByPrimaryKeySelective(record);
    }

    @Override
    public int updateByPrimaryKey(Department record) {
        return 0;
    }
}